package cn.wolfcode.trip.base.service.impl;
import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.domain.HotelContent;
import cn.wolfcode.trip.base.mapper.HotelContentMapper;
import cn.wolfcode.trip.base.mapper.HotelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HotelServiceImpl implements IHotelService {
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private HotelContentMapper hotelContentMapper;

    //列表
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Hotel> list = hotelMapper.queryForList(qo);
        return new PageInfo(list);
    }
    //增加或修改
    @Override
    public void saveOrUpdate(Hotel hotel) {
        if (hotel.getId()==null){
            hotelMapper.insert(hotel);
            //与酒店详情关联
            HotelContent hotelContent = hotel.getHotelContent();
            hotelContent.setId(hotel.getId());
            hotelContentMapper.insert(hotelContent);
        }else {
        hotelMapper.updateByPrimaryKey(hotel);
        HotelContent hotelContent = hotel.getHotelContent();
        hotelContent.setId(hotel.getId());
        hotelContentMapper.update(hotelContent);
        }
    }

    @Override
    public void delete(Long id) {
        hotelMapper.deleteByPrimaryKey(id);
    }
    //删除
    @Override
    public HotelContent getContent(Long id) {
        return hotelContentMapper.selectContent(id);
    }

    @Override
    public Hotel get(Long id) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        HotelContent hotelContent = hotelContentMapper.selectContent(id);
        hotel.setHotelContent(hotelContent);
        return hotel;
    }
}
